<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
include('../../includes/classes/core.php');

ini_set('memory_limit', '-1');

$action 	= $_REQUEST['act'];
$data 		= '';
$error		= '';
$start		= $_REQUEST['start'];
$end		= $_REQUEST['end'];
$client_id	= $_REQUEST['id'];

switch ($action) {
	case 'get_list':
		$count = $_REQUEST['count'];
		$rResult = mysql_query("
                     SELECT        CONCAT(b_letter_details.a_letter_id,'-',b_letter_details.barcode),
                    		        IF(SUBSTR(b_letter_details.barcode, 1, 1)=1, 'რეზერვი', 'კლიენტი') AS `type`,
                    				b_letter_details.barcode,
                    				production.`name` AS `production`,
                    				CONCAT('ა - ', b_letter_details.a_letter_id) AS `letter`,
                    				a_letter.fact_end_date,
                    				client.`name`,
		                            invoice.serial_number,
                    				IF(max(warehouse_detail.type)=2,1,0),
                    				COUNT(part1.id) AS toneti,
                    				COUNT(part2.id) AS barabani,
                    				COUNT(part3.id) AS magnituri,
                    				COUNT(part4.id) as rezini,
                    				COUNT(part5.id) as gamwmendi,
                    				COUNT(part6.id) as dozirebis,
                    				COUNT(part7.id) AS chipi,
                    				COUNT(part8.id) AS korpusi,
                    		        COUNT(part9.id) AS not_service,
                    				sum(IF(warehouse_detail.type=2 AND b_letter_details.part_type_id=2,0.00,b_letter_details.price)),
                    				sum(b_letter_details.self_cost),
                    		        sum(IF(warehouse_detail.type=2 AND b_letter_details.part_type_id=2,0,b_letter_details.price)) - sum(b_letter_details.self_cost) as diff
                    FROM `b_letter_details`
                    JOIN  a_letter on b_letter_details.a_letter_id=a_letter.id
                    LEFT JOIN  production ON SUBSTR(b_letter_details.barcode, 6, 4) = production.id
                    LEFT JOIN  client ON SUBSTR(b_letter_details.barcode, 2, 4) = client.id
		            LEFT JOIN  invoice    ON invoice.id=invoice_id   
		            LEFT JOIN  warehouse_detail ON b_letter_details.warehouse_detail_id=warehouse_detail.id
                    LEFT JOIN  b_letter_details part1 ON  part1.part_type_id=2  AND part1.id =b_letter_details.id
                    LEFT JOIN  b_letter_details part2 ON  part2.part_type_id=3  AND part2.id =b_letter_details.id
                    LEFT JOIN  b_letter_details part3 ON  part3.part_type_id=4  AND part3.id =b_letter_details.id
                    LEFT JOIN  b_letter_details part4 ON  part4.part_type_id=5  AND part4.id =b_letter_details.id
                    LEFT JOIN  b_letter_details part5 ON  part5.part_type_id=6  AND part5.id =b_letter_details.id
                    LEFT JOIN  b_letter_details part6 ON  part6.part_type_id=7  AND part6.id =b_letter_details.id
                    LEFT JOIN  b_letter_details part7 ON  part7.part_type_id=8  AND part7.id =b_letter_details.id
                    LEFT JOIN  b_letter_details part8 ON  part8.part_type_id=9  AND part8.id =b_letter_details.id
                    LEFT JOIN  b_letter_details part9 ON  part9.part_type_id=16 AND part9.id =b_letter_details.id
                    WHERE      DATE(a_letter.fact_end_date) BETWEEN '$start' AND '$end'
                     GROUP BY b_letter_details.a_letter_id, b_letter_details.barcode");
		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$data['aaData'][] = $aRow;
		}

		$data[sum][]= GetSums($start, $end, $data['aaData']);
        break;

	case 'GetSums' :
		$page	= GetSums($start, $end);
		$data = array('sums'	=> $page);

		break;
	case 'get_edit_page':
	  $arr=explode('-',$_REQUEST[id]);
	//  return 0;

	    $table="";
	   $rResult = mysql_query("SELECT
                cartridge_parts_type.`name`,
                b_letter_details.self_cost
                FROM b_letter_details
                JOIN cartridge_parts_type on b_letter_details.part_type_id=cartridge_parts_type.id
                WHERE barcode=$arr[1] AND a_letter_id=$arr[0]");
	   while ( $aRow = mysql_fetch_array( $rResult )){

	       $table.=$aRow[0]."-".$aRow[1]."<br />";

	   }
	   $data[page]=$table;
	    break;
    default:
       $error = "action null";
}

function GetSums($start, $end, $aRow){
$data = '

				<tr>
	        		<td>საგარანტიო</td>
	        		<td>' . array_sum($aRow[sagarantio] ). ' ც</td>
	        		<td style="background:none !important; border:none !important;">&nbsp;</td>
	        		<td>სულ.რაოდენობა</td>
	        		<td>' . $aRow['sum'] .  ' ც</td>
	        	</tr>
	        	<tr>
	        		<td>ტონერი</td>
	        		<td>' . array_sum($aRow[toneti] ) . ' ც</td>
	        		<td style="background:none !important; border:none !important;">&nbsp; &nbsp;</td>
	        		<td>სულ თვითღირებულება</td>
	        		<td>' . $aRow['self'] .  ' ლ</td>
	        	</tr>
	        	<tr>
	        		<td>ბარაბანი</td>
	        		<td>' . $aRow['part3'] . ' ც</td>
	        		<td style="background:none !important; border:none !important;">&nbsp;</td>
	        		<td>დ. მოგება</td>
	        		<td>' . $aRow[diff] .  ' ლ</td>
	        	</tr>
	        	<tr>
	        		<td>მაგნ. ლილვი</td>
	        		<td>' . $aRow['part4'] . ' ც</td>
	        	</tr>
	        	<tr>
	        	<td>მაგნ. ლილვი</td>
	        		<td>' . $aRow['part5'] . ' ც</td>
	        	</tr>
		    ';
	return $data;
}


$data['error'] = $error;

echo json_encode($data);

?>